package com.wgts.wgts.mvp.view.activity.guessing;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.RecyclerView;
import android.text.Html;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.SpannableStringBuilder;
import android.text.Spanned;
import android.text.style.ForegroundColorSpan;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.wgts.wgts.R;

import com.wgts.wgts.netmodle.resp.GuessingListEntity;
import com.wgts.wgts.tools.AppInfor;
import com.wgts.wgts.tools.MoHuGlide;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/*
 *
 *
 *   data:2018/10/417:12
 *
 */public class GuessingChildRecycleAdapter extends RecyclerView.Adapter<GuessingChildRecycleAdapter.ViewHold> {
    private Context context;
    private List<GuessingListEntity.Product> list;

    public GuessingChildRecycleAdapter(Context context, List<GuessingListEntity.Product> list) {
        this.context = context;
        this.list = list;
    }


    @NonNull
    @Override
    public ViewHold onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        return new ViewHold(LayoutInflater.from(context).inflate(R.layout.guessing_shopping_item, viewGroup, false));
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHold viewHold, final int i) {
        MoHuGlide.getInstance().glideImg(context, list.get(i).productSmallImage, viewHold.shopImage);
        viewHold.shopName.setText(list.get(i).productName);
        viewHold.shopPrice.setText("￥" + list.get(i).productPrice);
        String SguessingRound = "活动场次" + list.get(i).shareCount + "场";
        Spannable span = new SpannableString(SguessingRound);
        span.setSpan(new ForegroundColorSpan(ContextCompat.getColor(context, R.color.red_bg)), 4, (span.length()-1), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        viewHold.guessingRound.setText(span);
        if (AppInfor.getInstance().getUserState() < 2 || AppInfor.getInstance().getRefreshToken().equals("")) {
            viewHold.joinButtonText.setText("发起活动");
        } else {
            switch (list.get(i).status) {
                //未开始
                case "PENDING":
                    viewHold.joinButtonLy.setBackgroundResource(R.drawable.guessing_no_user_border);
                    viewHold.joinButtonText.setText("暂不可用");
                    break;
                //可发起
                case "INITIATE":
                    viewHold.joinButtonLy.setBackgroundResource(R.drawable.change_guessing_join_border);
                    viewHold.joinButtonText.setText("发起");
                    break;
                //进行中
                case "ACTIVE":
                    viewHold.joinButtonLy.setBackgroundResource(R.drawable.change_guessing_join_border);
                    viewHold.joinButtonText.setText("继续邀请");
                    break;
                //未开奖
                case "UNOPENED":
                    viewHold.joinButtonLy.setBackgroundResource(R.drawable.change_guessing_join_border);
                    viewHold.joinButtonText.setText("等待开奖");
                    break;
            }
        }
        viewHold.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
//                Intent intent = new Intent(context, MoreMoreGoodInfoActivity.class);
//                intent.putExtra(MoreMoreGoodInfoActivity.DRAWID, list.get(i).drawId);
//                intent.putExtra(MoreMoreGoodInfoActivity.TYPE, MoreMoreGoodInfoActivity.GUESS_LIST);
//                ((Activity) context).startActivityForResult(intent, 0);
            }
        });
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    public class ViewHold extends RecyclerView.ViewHolder {
        @BindView(R.id.shop_image)
        ImageView shopImage;
        @BindView(R.id.shop_name)
        TextView shopName;
        @BindView(R.id.guessing_round)
        TextView guessingRound;
        @BindView(R.id.shop_price)
        TextView shopPrice;
        @BindView(R.id.join_button_text)
        TextView joinButtonText;
        @BindView(R.id.join_button_ly)
        RelativeLayout joinButtonLy;
        public View itemView;

        public ViewHold(@NonNull View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
            this.itemView = itemView;
        }
    }


}
